package cn.sola.redis;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.sola.redis.vo.User;

/**   
 * 字符串类型的操作测试
 * @Package: cn.sola.redis 
 * @author: kangxiaohui   
 * @date: 2017年9月1日 下午3:26:32 
 * 针对<String,Object>类型的结果处理 
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-redis3.xml")
public class StringRedisTemplate {

	@Resource(name = "redisTemplate")
	private RedisTemplate<String, User> redisTemplate;
	
	/**
	 * 某个key时候存在
	 */
	@Test
	public void hasKey(){
		boolean flag = redisTemplate.hasKey("user_1231310113");
		System.out.println(flag);
	}
	
	/**
	 * 增加User对象到缓存
	 */
	@Test
	public void addUser(){
		ValueOperations<String, User> operations = redisTemplate.opsForValue();
		User user = new User();
		user.setName("sola");
		user.setPassword("12345e");
		operations.set("user_1231310113", user);
	}
	
	/**
	 * 得到User实体
	 */
	@Test
	public void getUser(){
		ValueOperations<String, User> operations = redisTemplate.opsForValue();
		User user = operations.get("user_1231310113");
		System.out.println(user);
	}
}
